#include <bits/stdc++.h>
// 2025/02/03
// tag:
// Author: Zhang Muen
using namespace std;

int n, p, enter[101], u[101], C[101];
unordered_map<int, int> graph[101];

signed main()
{
    cin >> n >> p;
    for (int i = 1; i <= n; i++)
        cin >> C[i] >> u[i];
    for (int i = 1; i <= p; i++) {
        int x, y, w;
        cin >> x >> y >> w;
        graph[x][y] = w;
        enter[y]++;
    }
    queue<int> q;
    set<int> ans;
    for (int i = 1; i <= n; i++)
        if (!enter[i])
            q.push(i);
        else
            C[i] -= u[i];
    while (!q.empty()) {
        int s = q.size();
        ans.clear();
        for (int i = 0; i < s; i++) {
            int cur = q.front();
            ans.insert(cur);
            q.pop();
            for (auto it : graph[cur]) {
                int v = it.first;
                int w = it.second;
                if (C[cur] > 0)
                    C[v] += C[cur] * w;
                enter[v]--;
                if (!enter[v])
                    q.push(v);
            }
        }
    }
    int cnt = 0;
    for (auto it : ans)
        if (C[it] > 0)
            cout << it << " " << C[it] << endl, cnt++;
    if (cnt == 0)
        cout << "NULL" << endl;
    return 0;
}